from app import db
import datetime

#后台用户表
class Admin(db.Model):
    __tablename__ = 'admin'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    username = db.Column(db.String(30),nullable=False,unique=True,comment="用户名")
    password = db.Column(db.String(30),nullable=False,comment="密码")
    userPhoto = db.Column(db.String(200),nullable=True,comment="用户头像")
    phone = db.Column(db.String(20),nullable=True,comment="用户手机",default="暂无信息")
    name = db.Column(db.String(20),nullable=True,comment="名字")
    nick = db.Column(db.String(30), nullable=True, comment="用户昵称")
    sex =  db.Column(db.Boolean, nullable=False, default=True,comment='性别')
    address = db.Column(db.String(150), nullable=True, comment="住址")
    wechat = db.Column(db.String(70), nullable=True, comment="微信")
    birthdate = db.Column(db.Date, nullable=True, comment="生日", default="暂无信息")
    intro = db.Column(db.String(255),nullable=True,default="这个人很懒,什么都没有留下！",comment="个人简介")
    flag = db.Column(db.Boolean,nullable=False,comment="0 封停 1 正常")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    initial = db.Column(db.Boolean, nullable=False,default=False, comment="初始")
    jid = db.Column(db.ForeignKey('admin_job.id'),nullable=True,comment='职位')
    job = db.relationship('Admin_job', backref='admin')
    log = db.relationship('Admin_login_log', backref='admin')

#后台登录记录表
class Admin_login_log(db.Model):
    __tablename__ = 'admin_login_log'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    addr = db.Column(db.String(50),nullable=False,comment="登录ip")
    country = db.Column(db.String(20),nullable=True,comment="国家")
    regionName = db.Column(db.String(20),nullable=True,comment="省份")
    city = db.Column(db.String(20),nullable=True,comment="城市")
    aid = db.Column(db.ForeignKey('admin.id'), nullable=True, comment='职位')
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')

#后台操作记录表
class Admin_operate_log(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    func = db.Column(db.String(30),nullable=False,comment="功能")
    up = db.Column(db.Integer,nullable=False,comment="1：添加，2：修改 3：删除 4：上线下")
    comment = db.Column(db.JSON,nullable=False,comment="备注")
    createtime = db.Column(db.DateTime, default=datetime.datetime.now, nullable=False, comment="创建时间")
    aid = db.Column(db.ForeignKey('admin.id'), nullable=True, comment='发布人')

#后台职位表
class Admin_job(db.Model):
    __tablename__ = 'admin_job'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    jobName = db.Column(db.String(30), nullable=False, unique=True, comment="用户名")
    func = db.Column(db.Boolean, nullable=False, comment="修改功能权限")
    user = db.Column(db.Boolean,nullable=False,comment="修改用户权限")
    imgText = db.Column(db.Boolean,nullable=False,comment="图文编辑权限")
    price = db.Column(db.Boolean,nullable=False,comment="价格调整权限")
    allot = db.Column(db.Boolean, nullable=False, comment="用户分配权限")
    sell = db.Column(db.Boolean, nullable=False, comment="是否属于销售")
    Seo = db.Column(db.Boolean,nullable=False,comment="SEO数据分析权限")
    account  = db.Column(db.Boolean,nullable=False,comment="后台账号管理")
    initial = db.Column(db.Boolean, nullable=False, comment="初始")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

#后台cookies表
class Admin_Cookies(db.Model):
    __tablename__ = 'admin_cookie'
    id = db.Column(db.Integer,autoincrement=True,primary_key=True,index=True)
    cookies = db.Column(db.String(200),nullable=False,comment='登陆凭证')
    endTime = db.Column(db.DateTime,nullable=False,comment='时间戳')
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    admin_id = db.Column(db.Integer,db.ForeignKey('admin.id'))
    admin = db.relationship('Admin', backref='acookies')

#后台配置表
class Config(db.Model):
    __tablename__ = 'config'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True, index=True)
    key = db.Column(db.String(40),nullable=False)
    value = db.Column(db.JSON,nullable=True)
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)


#轮播图数据
class ViewPager(db.Model):
    __tablename__ = 'view_pager'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True, index=True)
    zone = db.Column(db.Integer, nullable=False, comment='1:首页')
    name = db.Column(db.String(225),nullable=False,comment="轮播名")
    imgName = db.Column(db.String(200),nullable=False,comment="图片名称")
    href = db.Column(db.TEXT,nullable=True,comment="跳转链接")
    rank = db.Column(db.Integer,nullable=False,default=1,comment="排序")
    status = db.Column(db.Boolean,nullable=False,default=True,comment="状态")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

#功能数据
class FuncData(db.Model):
    __tablename__ = 'func_data'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True, index=True)
    name = db.Column(db.String(60),nullable=False,comment="功能名")
    integral = db.Column(db.Integer,nullable=False,comment="扣费积分")
    imgName = db.Column(db.String(200), nullable=True, comment="图片名称")
    href = db.Column(db.TEXT, nullable=True, comment="跳转链接")
    rank = db.Column(db.Integer, nullable=False, default=1, comment="排序")
    status = db.Column(db.Boolean, nullable=False, default=True, comment="状态")
    comment = db.Column(db.TEXT,nullable=True,comment="备注")
    content = db.Column(db.TEXT,nullable=True,comment="使用说明")
    intro = db.Column(db.String(40), nullable=True, comment="功能简介")
    useCount = db.Column(db.Integer,nullable=False,comment="初始使用数量")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

#文章表
class Article(db.Model):
    __tablename__ = 'article'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True, index=True)
    title = db.Column(db.String(225),nullable=False,comment="标题")
    content = db.Column(db.TEXT,nullable=False,comment="标题")
    category = db.Column(db.Integer,nullable=False,comment="1:运营案例 2电商资讯 3学习园地 4直通车")
    cover = db.Column(db.String(225),nullable=True,comment="封面")
    read = db.Column(db.Integer,nullable=False,comment="初始阅读数量")
    intro = db.Column(db.TEXT, nullable=False, comment="文章简介")
    original = db.Column(db.Boolean, nullable=False, comment="是否原创")
    Istop = db.Column(db.Boolean, nullable=False, comment="是否置顶")
    flag = db.Column(db.Integer,nullable=False,comment="1:发布 2:下线 3:草稿")
    releaseTime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='发布时间')
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
    admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))
    admin = db.relationship('Admin', backref='article')

#视频表
class Video(db.Model):
    __tablename__ = 'video'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True, index=True)
    title = db.Column(db.String(225),nullable=False,comment="标题")
    persistentId= db.Column(db.String(60),nullable=True,comment="预转码id")
    videoName = db.Column(db.String(225), nullable=True, comment="视频源")
    cover = db.Column(db.String(225), nullable=False, comment="封面")
    coverList = db.Column(db.JSON, nullable=True, comment="封面")
    live = db.Column(db.String(225),nullable=True,comment="m3u8流")
    read = db.Column(db.Integer,nullable=False,comment="初始观看数量")
    intro = db.Column(db.TEXT, nullable=False, comment="视频简介")
    duration = db.Column(db.Float, nullable=False, comment="视频时长")
    original = db.Column(db.Boolean, nullable=False, comment="是否原创")
    Istop = db.Column(db.Boolean, nullable=False, comment="是否置顶")
    flag = db.Column(db.Integer,nullable=False,comment="1:发布 2:下线 3:转码中 4:转码失败")
    isVip = db.Column(db.Boolean, nullable=False,default=False, comment="是否设定会员可看")
    releaseTime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='发布时间')
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
    admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))
    admin = db.relationship('Admin', backref='video')

#客服列表
class Service(db.Model):
    __tablename__ = 'service'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True, index=True)
    qr = db.Column(db.String(225),nullable=False,comment="二维码")
    title = db.Column(db.String(20),nullable=False,comment="二维码")
    name = db.Column(db.String(10),nullable=False,comment="姓名")
    flag = db.Column(db.Boolean,nullable=False)
    time = db.Column(db.String(20),nullable=False,comment="时间")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
    rotateTime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')

#会员等级
class UserRank(db.Model):
    __tablename__ = 'user_rank'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True, index=True)
    name = db.Column(db.String(10),nullable=False,unique=True,comment="会员等级")
    level = db.Column(db.Integer,nullable=False,comment="等级")
    color = db.Column(db.String(60),nullable=True,comment="字体颜色")
    background = db.Column(db.String(60),nullable=True,comment="背景颜色")
    price = db.Column(db.Float,nullable=False,comment="单价")
    initial = db.Column(db.Boolean,nullable=False,comment="初始")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

#会员功能列表
class RankFunc(db.Model):
    __tablename__ = 'rank_func'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True, index=True)
    fid = db.Column(db.ForeignKey('func_data.id'), nullable=False, comment='功能')
    rid = db.Column(db.ForeignKey('user_rank.id'), nullable=False, comment='会员')
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
    count = db.Column(db.Integer,nullable=False,comment="限制次数")
    func = db.relationship('FuncData', backref='rf')
    urank = db.relationship('UserRank', backref='rf')

#会员套餐列表
class RankMeal(db.Model):
    __tablename__ = 'rank_meal'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True, index=True)
    rid = db.Column(db.ForeignKey('user_rank.id'), nullable=False, comment='会员')
    name = db.Column(db.String(10),nullable=False,comment="套餐名")
    price = db.Column(db.Float,nullable=False,comment="套餐价格")
    rate  = db.Column(db.Float,nullable=True,comment="折扣")
    rateName = db.Column(db.String(10),nullable=True,comment="折扣名称")
    rateFlag = db.Column(db.Boolean,nullable=False,comment="启用折扣")
    days = db.Column(db.Integer,nullable=False,comment="充值时间/天")
    sort = db.Column(db.Integer,nullable=False,comment="排序")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
    rank = db.relationship('UserRank', backref='meal')

class Visitor(db.Model):
    __tablename__ = 'visitor'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    addr = db.Column(db.String(80),nullable=True,comment="ip")
    sign = db.Column(db.String(40),nullable=False,comment="访客标示")
    keyword = db.Column(db.String(225),nullable=True,comment="进站词")
    landing = db.Column(db.String(225),nullable=True,comment="落地页")
    leave =  db.Column(db.String(225),nullable=True,comment="离开页面")
    referer = db.Column(db.String(225),nullable=True,comment="来源页")
    source = db.Column(db.String(225),nullable=True,comment="落地页")
    islogin = db.Column(db.Boolean,nullable=True,comment="是否注册",default=False)
    city = db.Column(db.String(80),nullable=True,comment="城市")
    province = db.Column(db.String(80), nullable=True, comment="省")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')


#积分套餐列表
class IntegralMeal(db.Model):
    __tablename__ = 'integral_meal'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True, index=True)
    name = db.Column(db.String(10),nullable=False,comment="套餐名")
    price = db.Column(db.Float,nullable=False,comment="套餐价格")
    integral = db.Column(db.Integer,nullable=False,comment="充值积分")
    give = db.Column(db.Integer,nullable=False,comment="赠僧积分")
    rate  = db.Column(db.Float,nullable=True,comment="折扣")
    rateName = db.Column(db.String(10),nullable=True,comment="折扣名称")
    rateFlag = db.Column(db.Boolean,nullable=False,comment="启用折扣")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)


#用户销售信息表
class UserSell(db.Model):
    __tablename__ = 'user_sell'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    keyword = db.Column(db.String(225),nullable=True,comment="关键词")
    wechat = db.Column(db.String(60),nullable=True,comment="用户名")
    source = db.Column(db.String(225), nullable=True, comment="来源")
    qq = db.Column(db.String(60),nullable=True,comment="用户名")
    call_status = db.Column(db.Integer,nullable=False,comment="0:未拨打 1:已拨打 2:拒接/挂断 3:停机",default=0)
    wechat_status = db.Column(db.Integer,nullable=False,comment="0:未加微信 1:待加微信 2:已加微信",default=0)
    Intent_status = db.Column(db.Integer,nullable=False,comment="0:暂无意向 1:有意向 2:奇葩客户 3:非卖家",default=0)
    user = db.relationship('User', backref='sell')

#用户合作套餐表
class UserCoop(db.Model):
    __tablename__ = 'user_coop'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    phone = db.Column(db.String(30),nullable=False,comment="合作手机号")
    name = db.Column(db.String(30),nullable=True,comment="合作用户姓名")
    wechat = db.Column(db.String(60),nullable=True,comment="合作用户微信号")
    shopName = db.Column(db.String(30),nullable=True,comment="合作用户店铺名")
    coopName = db.Column(db.String(200), nullable=True, comment="合作套餐名")
    price = db.Column(db.Float,nullable=False,comment="收款")
    startTime = db.Column(db.Date, nullable=False, comment='开始时间')
    endTime = db.Column(db.Date, nullable=False, comment='结束时间')
    comment = db.Column(db.TEXT,nullable=True,comment="备注")
    payType = db.Column(db.Integer,nullable=False,comment="0:支付宝支付 2：微信支付 3：银行转账 4：其他方式")
    payorder = db.Column(db.String(225),nullable=False,comment="支付交易流水号")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    aid = db.Column(db.ForeignKey('admin.id'), nullable=True, comment='绑定后台销售')
    admin = db.relationship('Admin', backref='coop')

#销售每日记录表
class SellDays(db.Model):
    __tablename__ = 'sell_days'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    uid = db.Column(db.ForeignKey('user.id',ondelete='CASCADE'), nullable=True, comment='绑定用户')
    aid = db.Column(db.ForeignKey('admin.id',ondelete='CASCADE'), nullable=True, comment='绑定后台销售',)
    call_status = db.Column(db.Integer, nullable=True, comment="用户名")
    wechat_status = db.Column(db.Integer, nullable=True, comment="用户名")
    Intent_status = db.Column(db.Integer, nullable=True, comment="0:暂无意向 1:有意向 2:奇葩客户 3:非卖家")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')

class UserComment(db.Model):
    __tablename__ = 'user_comment'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    uid = db.Column(db.ForeignKey('user.id'), nullable=True, comment='用户')
    aid = db.Column(db.ForeignKey('admin.id'), nullable=True, comment='销售')
    content = db.Column(db.TEXT,nullable=False,comment="内容")
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    user = db.relationship('User', backref='comment')
    admin = db.relationship('Admin', backref='comment')


#用户表
class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    username = db.Column(db.String(30),nullable=False,unique=True,comment="用户名")
    phone = db.Column(db.String(20), nullable=False,unique=True, comment="用户手机")
    integral = db.Column(db.Integer,nullable=False,comment="用户积分",default=0)
    password = db.Column(db.String(30),nullable=True,comment="密码")
    userPhoto = db.Column(db.String(200),nullable=True,comment="用户头像")
    sex = db.Column(db.Boolean,nullable=False,default=True,comment="性别")
    nick = db.Column(db.String(30),nullable=True,comment="用户名")
    aid = db.Column(db.ForeignKey('admin.id'), nullable=True, comment='绑定后台销售')
    sid = db.Column(db.ForeignKey('user_sell.id'), nullable=True, comment='绑定销售信息')
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    admin = db.relationship('Admin', backref='user')


#用户登录日志
class User_login_log(db.Model):
    __tablename__ = 'user_login_log'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    addr = db.Column(db.String(50),nullable=False,comment="登录ip")
    city = db.Column(db.String(80), nullable=True, comment="城市")
    province = db.Column(db.String(80), nullable=True, comment="省")
    uid = db.Column(db.ForeignKey('user.id'), nullable=True, comment='用户')
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')

#后台cookies表
class User_Cookies(db.Model):
    __tablename__ = 'user_cookie'
    id = db.Column(db.Integer,autoincrement=True,primary_key=True,index=True)
    cookies = db.Column(db.String(200),nullable=False,comment='登陆凭证')
    endTime = db.Column(db.DateTime,nullable=False,comment='时间戳')
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    uid = db.Column(db.Integer,db.ForeignKey('user.id'))
    user = db.relationship('User', backref='cookies')

class UserLevel(db.Model):
    __tablename__ = 'user_level'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    rid = db.Column(db.ForeignKey('user_rank.id'), nullable=True, comment='会员等级')
    uid = db.Column(db.ForeignKey('user.id'), nullable=True, comment='会员绑定')
    user = db.relationship('User', backref=db.backref('levels', lazy='dynamic'))
    rank = db.relationship('UserRank', backref='ulevel')
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now, comment='创建时间')
    endTime = db.Column(db.Date, nullable=False, comment='到期时间')
    updateTime = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)


